#include <iostream>

using namespace std;

template<typename T>
class MyVector
{
public:
    MyVector(int size = 10)  :length(size),current_pos(0)
    {
        data = new int[current_pos];       //根据size分配空间
    }

    ~MyVector(){
        clear();
    }
   
    // 访问
    T& at(int index) const{       return data[index];}
    T& front() const{             return data[0]; }
    T& back() const{              return data[current_pos]; }
    T& operator[](int index) {    return data[index];   };

    // 容量
    bool empty() const {
			return current_pos==0;
		}
    int size() const{
        return current_pos;
    }
    int capacity() const{
        return length-current_pos;
    }

    // 修改
    void clear() {
		if (data == nullptr) 
				return;
        delete[] data;
		data = nullptr;

	}
    void insert(int index, const T& d){
      
           for(int i=length-1; i>=index; --i){
               data[i+1] = data[i];
           }
           data[index] = d;
       
       ++current_pos;
    }

    void remove(int index){
        int i=current_pos;
        while(i>=0){
            if(data[i]==index){
                for(int j=i; j<=current_pos; ++j){
                     data[j] =data[j+1];
                 }
                --current_pos;
            }
            i--;
        }
    }
    void push_back(const T& d){
        data[current_pos++] = d;
    }
    void pop_back(){
       current_pos--;
    }

private:
    T* data;           //数据指针
    int length;        //数组的尺寸
    int current_pos;   //实际容量（即分配的空间）
};      


int main(){
    
    MyVector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
   cout<< vec.size()<<endl;
    vec.pop_back();
   cout<< vec.back()<<" "<< vec.front()<<endl;
    vec.insert(2,9);
   cout<< vec.at(2)<<endl;
    vec.remove(9);
    vec.clear();

   return 0;
}